//
//  StandFocuViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/19.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation


class StandFocuViewController: BaseViewController {
    
    var info:UserInfo?
    
    override func loadView() {
        let view = NSView.init(frame: NSRect.init(x: 0, y: 0, width: kStandWidth, height: kStandHight))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.black.cgColor
        self.view = view
    }
    override func viewWillAppear() {
        super.viewWillAppear()
        NotificationCenter.default.addObserver(self, selector: #selector(userStutasChangs(not:)), name: MTUserStateNotification, object: nil)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
    }
    
    func loadData() {
            if TCGirdRender.shared().renderer.infoList[0].userId?.isCurret() == true {
                if Td.sh().lc.isVdOpen {
                    TXLiteManager.shared().stopLocalPreview()
                    TXLiteManager.shared().startLocalPreview(view: self.videoView.videoView)
                }else {
                    TXLiteManager.shared().stopLocalPreview()
                }
                
                let isMute = MTNot.getLocalMute()
                self.videoView.isMute = isMute
                TXLiteManager.shared().muteLocalAudio(isMute)
            }
        
    }
    
    func setupUI() {
        self.view.layer?.backgroundColor = color_dark60.cgColor
        
        self.view.addSubview(videoView)
        videoView.snp.makeConstraints { (make) in
            make.width.bottom.centerX.equalToSuperview()
            make.top.equalTo(40)
        }
        
        self.view.addSubview(leftDrop)
        leftDrop.snp.makeConstraints { (make) in
            make.bottom.left.equalToSuperview()
            make.width.height.equalTo(10)
        }
        
        self.view.addSubview(rightDrop)
        rightDrop.snp.makeConstraints { (make) in
            make.bottom.right.equalToSuperview()
            make.width.height.equalTo(10)
        }
    }
    
    lazy var leftDrop: NSImageView = {
        let view = NSImageView.init(image: NSImage.image(name:"icon_drop_left")!)
        return view
    }()
    
    lazy var rightDrop: NSImageView = {
        let view = NSImageView.init(image: NSImage.image(name:"icon_drop_right")!)
        return view
    }()
    
    lazy var videoView: TCVideoView = {
        var info = Td.sh().lc
        let view = TCVideoView.init(data: info, delegate: nil)
        return view
    }()
}

extension StandFocuViewController {
    @objc func userStutasChangs(not:Notification)  {
        guard let mode = not.object as? UserMode else {
            return
        }
        if mode.userId == nil {
            if let isMute = mode.isMute {
                self.videoView.isMute = isMute
                TXLiteManager.shared().muteLocalAudio(isMute)
            }
            
            if let isVdOpen = mode.isVdOpen {
                Td.sh().lc.isVdOpen = isVdOpen
                if isVdOpen == true {
                    TXLiteManager.shared().stopLocalPreview()
                    TXLiteManager.shared().startLocalPreview(view: self.videoView.videoView)
                }else {
                    TXLiteManager.shared().stopLocalPreview()
                }
            }
        }
    }
}
